<?php
/******************************************************************************************************************
 
Plugin Name: wpmu-blogcategories

Plugin URI: http://code.google.com/p/certiopensource/

Description: WordPressMU plugin for classifying blogs into categories. Developed in the style of wpmu-topposts.

Version: 0.2

Author: Ezequiel Conte

Copyright:

    Copyright 2007 Fundação CERTI

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

            
*******************************************************************************************************************/

class certi_blog_categories 
{
    var $table_categories;
	
	function certi_blog_categories() 
	{	
		global $table_prefix,$wpdb;

		$this->table_categories  = $wpdb->blogs . "_certi_categories";

	}
	
    function setup() 
    {
	    global $wpdb;
	    
	    //check if table was already created
	    if($wpdb->get_var("show tables like '$this->table_categories'") != $this->table_categories) {

	    	$table_categories_query = "CREATE TABLE $this->table_categories (
						      id int(11) unsigned NOT NULL auto_increment,
						      category_name VARCHAR(55) NOT NULL default '',
						      UNIQUE KEY id (id)
						      )";	  
 			
 			$results = $wpdb->query($table_categories_query);
 			
 			
			//insert categories into database
		    $categories = array('Pessoal','Centro', 'Projeto', 'Assunto');
		    
		    
		    foreach ( $categories as $category ) {
		    	$insert = "INSERT INTO $this->table_categories (category_name) VALUES ('$category');";  
		    	$results = $wpdb->query( $insert );	
		    }
		    
		    
		    //add option blog_category to every blog
			$blog_list = $wpdb->get_results( "SELECT blog_id FROM $wpdb->blogs;");
			 
			foreach ( $blog_list as $blog ) {
				$this->set_blog_category($blog->blog_id,'1');
			}
		    
	    }
	    		
    }

    function get_blogs_from_category_id($category_id) 
    {
    	global $wpdb, $wpmuBaseTablePrefix;
    	
   		$blog_list = $wpdb->get_results( "SELECT blog_id FROM $wpdb->blogs;");
		 
   		$blogs_from_category = array();
		foreach ( $blog_list as $blog ) {
			$category = $wpdb->get_var("SELECT option_value FROM " . $wpmuBaseTablePrefix . $blog->blog_id . "_options
						WHERE option_name LIKE 'blog_category';");
			if ($category == $category_id) {
				array_push($blogs_from_category, $blog);
			}
		}
		
		return $blogs_from_category;
    	
    }
    
        
    function get_blogs_from_category($category_name) 
    {
    	global $wpdb, $wpmuBaseTablePrefix;
    	
    	$category_id = $wpdb->get_var("SELECT id FROM $this->table_categories
						WHERE category_name LIKE '$category_name';");
    	
    	return $this->get_blogs_from_category_id($category_id);
    	
    }
    
	function get_blogs_from_category_id_html($category_id)
    {
    	foreach ( $this->get_blogs_from_category_id($category_id) as $blog )  {
    		$details = get_blog_details($blog->blog_id);
			echo "<li><a href='http://" . $details->domain . $details->path . "'>" . $details->blogname . "</a></li>";
    	}
    }
    
    function get_blogs_from_category_html($category_name)
    {
    	
    	foreach ( $this->get_blogs_from_category($category_name) as $blog )  {
    		$details = get_blog_details($blog->blog_id);
			echo "<li><a href='http://" . $details->domain . $details->path . "'>" . $details->blogname . "</a></li>";
    	}
    }
    
	function get_category_name($category_id)
    {
    	global $wpdb;
    	return $wpdb->get_var("SELECT category_name FROM $this->table_categories
						WHERE id = '$category_id';");
    }
    
	function get_categories()
    {
    	global $wpdb;
    	
    	return $wpdb->get_results("SELECT id, category_name FROM $this->table_categories;");
    }
    
	function get_categories_html()
    {
    	foreach ( $this->get_categories() as $category )  {
			echo "<li><a href='" .get_settings('siteurl'). "/blog_categories.php?idcat=$category->id'>" . $category->category_name . "</a></li>";
    	}
    }
    
	function get_categories_select()
    {
    	global $wpdb;
    	
    	$blog_category = $this->get_blog_category($wpdb->blogid);
    	
    	echo "<tr><th valign='top'>Blog Category:</th><td><select name='blog_category_id'>";
    	foreach ( $this->get_categories() as $category )  {
    		if ($blog_category && $blog_category == $category->id) {
    			$selected = "selected='selected'";
    		}
			echo "<option value='$category->id' ". $selected .">" . $category->category_name ."</option>";
			$selected = '';
    	}
    	echo "</select></td></tr>";
    }
    
	function set_new_blog_category($blog_id)
    {
    	$category_id = $_POST['blog_category_id'];

    	$this->set_blog_category($blog_id,$category_id);
    }
    
    
    function set_blog_category($blog_id, $category_id = '1') 
    {
    	global $wpdb, $wpmuBaseTablePrefix;
    	
    	if ($wpdb->get_var("select option_name from ". $wpmuBaseTablePrefix . $blog_id . "_options where option_name like 'blog_category';") == 'blog_category') {
    		$update = "UPDATE " . $wpmuBaseTablePrefix . $blog_id . "_options 
						SET option_value='$category_id' WHERE option_name LIKE 'blog_category';";  
			$results = $wpdb->query( $update );	
    	} else {
			$insert = "INSERT INTO " . $wpmuBaseTablePrefix . $blog_id . "_options 
						(option_name,option_value) VALUES ('blog_category','$category_id');";  
			$results = $wpdb->query( $insert );	
    	}
    }
    
    function get_blog_category($blog_id) 
    {
    	global $wpdb, $wpmuBaseTablePrefix;
    	
    	return $wpdb->get_var("select option_value from ". $wpmuBaseTablePrefix . $blog_id . "_options where option_name like 'blog_category';");
    }
    
    function add_submenu()
    {
    	add_options_page('Blog Category Configuration', 'Blog Category', 10, 'certi_blog_category', array(&$this,'config_page'));
    }
    
    function config_page() 
    {
    	global $wpdb;
    	
    	if ($_POST['action'] == 'update') {
			$this->set_blog_category($wpdb->blogid,$_POST['blog_category_id']);
			$updated = true;
    	}
    	if ($updated) { ?>
<div id="message" class="updated fade"><p><?php _e('Options saved.') ?></p></div>
<?php	} ?>
<div class="wrap">
<h1>Blog Category</h1>
<form name="blogcategoryform" action="" method="post">
	<table width="100%"  class="editform">
	<?php
			$this->get_categories_select();
	?>
	</table>
	<p class="submit">
	<input type="hidden" name="action" value="update" /> 
	<input type="submit" name="Submit" value="<?php _e('Update Options') ?> &raquo;" /> 
	</p>
</form>
</div>
<?php
    }
};


$certi_wpmubc = new certi_blog_categories();
//find a better way to call setup.
$certi_wpmubc->setup();
	
	
add_action('signup_blogform', array(&$certi_wpmubc, 'get_categories_select'));
add_filter('wpmu_new_blog', array(&$certi_wpmubc, 'set_new_blog_category'));
add_action('admin_menu', array(&$certi_wpmubc, 'add_submenu'));


function certi_get_blogs_from_category_html($category_name = 'Pessoal')
{
    global $certi_wpmubc;
	return $certi_wpmubc->get_blogs_from_category_html($category_name);
}

function certi_get_blogs_from_category_id_html($category_id = '1')
{
    global $certi_wpmubc;
	return $certi_wpmubc->get_blogs_from_category_id_html($category_id);
}

function certi_get_category_name($category_id = '1')
{
    global $certi_wpmubc;
	return $certi_wpmubc->get_category_name($category_id);
}

function certi_get_categories_html()
{
    global $certi_wpmubc;
	return $certi_wpmubc->get_categories_html();
}


?>
